#ifndef UART_H
#define UART_H
/** 
 *  @defgroup nxbot_uart UART Library
 * @ingroup nxbot_low_level 
 *  @code #include <uart.h> @endcode
 * 
 *  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. 
 *
 *  This library can be used to transmit and receive data through the built in UART. 
 *
 *  An interrupt is generated when the UART has finished transmitting or
 *  receiving a byte. The interrupt handling routines use circular buffers
 *  for buffering received and transmitted data.
 *
 *  The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
 *  the size of the circular buffers in bytes. Note that these constants must be a power of 2.
 *  You may need to adapt this constants to your target and your application by adding 
 *  CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
 *  It is extremely recommended that you use a GCC version greater than 3.4, otherwise the behavior or even the compilation is not guaranteed!
 *
 *  @note Heavily based on UART library made by Peter Fleury (pfleury@gmx.ch  http://jump.to/fleury). 
 *  Modifications have been done for support of other microcontrollers, and support of other functions (like uart_puti).
 *  @author Alejandro Pustowka alejandro.pustowka@gmail.com 
 */
 
/**@{*/
#include <avr/io.h>


/*
** constants and macros
*/

/** @brief  UART Baudrate Expression
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz          
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
 */
#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-0.5)

/** @brief  UART Baudrate Expression for ATmega double speed mode
 *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz           
 *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     
 */
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)


/** @brief Size of the circular receive buffer, must be power of 2 */
#ifndef UART_RX_BUFFER_SIZE
#define UART_RX_BUFFER_SIZE 64
#endif
/** @brief Size of the circular transmit buffer, must be power of 2 */
#ifndef UART_TX_BUFFER_SIZE
#define UART_TX_BUFFER_SIZE 64
#endif

/* test if the size of the circular buffers fits into SRAM */
#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
#endif

/**
 * @brief high byte error return code of uart_getc()
 *
 * Frame error... meaning?
 */
#define UART_FRAME_ERROR      0x0800              /* Framing Error by UART       */
/**
 * @brief high byte error return code of uart_getc()
 *
 * Data overrun... meaning?
 */
#define UART_OVERRUN_ERROR    0x0400              /* Overrun condition by UART   */
/**
 * @brief high byte error return code of uart_getc()
 *
 * Overflow on buffer
 */
#define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */
/**
 * @brief high byte error return code of uart_getc()
 *
 * No data on buffer.
 */
#define UART_NO_DATA          0x0100              /* no receive data available   */


/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, no parity, 1 stop bit.
 */
#define UART_FORMAT_8D0P1S 0
/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, even parity, 1 stop bit.
 */
#define UART_FORMAT_8DEP1S 1
/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, odd parity, 1 stop bit.
 */
#define UART_FORMAT_8DOP1S 2
/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, no parity, 2 stop bits.
 */
#define UART_FORMAT_8D0P2S 3
/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, even parity, 2 stop bits.
 */
#define UART_FORMAT_8DEP2S 4
/**
 * @brief Constant definition for the data format.
 *
 * Data is composed of 8 bits, odd parity, 2 stop bits.
 */
#define UART_FORMAT_8DOP2S 5



/*
** function prototypes
*/

/**
   @brief   Initialize UART and set baudrate 
   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
   @param   format Define the format of the frame (parity, stop bits). 
            Use one of the defined formats, beginning with UART_FORMAT, e.g UART_FORMAT_8D0P1S
   @return  none
*/
extern void uart_init(unsigned int baudrate, unsigned char format);


/**
 *  @brief   Get received byte from ringbuffer
 *
 * Returns in the lower byte the received character and in the 
 * higher byte the last receive error.
 * UART_NO_DATA is returned when no data is available.
 *
 *  @param   void
 *  @return  lower byte:  received byte from ringbuffer
 *  @return  higher byte: last receive status
 *           - \b 0 successfully received data from UART
 *           - \b UART_NO_DATA           
 *             <br>no receive data available
 *           - \b UART_BUFFER_OVERFLOW   
 *             <br>Receive ringbuffer overflow.
 *             We are not reading the receive buffer fast enough, 
 *             one or more received character have been dropped 
 *           - \b UART_OVERRUN_ERROR     
 *             <br>Overrun condition by UART.
 *             A character already present in the UART UDR register was 
 *             not read by the interrupt handler before the next character arrived,
 *             one or more received characters have been dropped.
 *           - \b UART_FRAME_ERROR       
 *             <br>Framing Error by UART
 */
extern unsigned int uart_getc(void);


/**
 *  @brief   Put byte to ringbuffer for transmitting via UART
 *  @param   data byte to be transmitted
 *  @return  none
 */
extern void uart_putc(unsigned char data);

/**
 *  @brief   Put integer to ringbuffer for transmitting via UART
 *
 *  The LSB is bufferd first, then the MSB.
 *
 *  @param   data integer to be transmitted
 *  @return  none
 */
extern void uart_puti(int data);
/**
 *  @brief   Put string to ringbuffer for transmitting via UART
 *
 *  The string is buffered by the uart library in a circular buffer
 *  and one character at a time is transmitted to the UART using interrupts.
 *  Blocks if it can not write the whole string into the circular buffer.
 * 
 *  @param   s string to be transmitted
 *  @return  none
 */
extern void uart_puts(const char *s );

/**
 *  @brief   Put string to ringbuffer for transmitting via UART, definining number of characters to be sended.
 *
 *  The specified characters are buffered in the circular buffer and one character is transmitted at a time via the uart.
 *  Blocks if it can not write the whole string into the circular buffer.
 * 
 *  @param   s string to be transmitted
 *  @param   len number of bytes to be transmitted
 *  @return  none
 */
extern void uart_puts_len(const unsigned char *s,unsigned int len);


/**
 * @brief    Put string from program memory to ringbuffer for transmitting via UART.
 *
 * The string is buffered by the uart library in a circular buffer
 * and one character at a time is transmitted to the UART using interrupts.
 * Blocks if it can not write the whole string into the circular buffer.
 *
 * @param    s program memory string to be transmitted
 * @return   none
 * @see      uart_puts_P
 */
extern void uart_puts_p(const char *s );

/**
 *  @brief   Get string from UART to memory
 *
 *  It keeps receiving until a NULL character (0x00) is received in the uart.
 * 
 *  @param   s pointer to where the string will be saved.
 *  @return  same as uart_getc()
 */
extern char uart_gets(char* s);
/**
 * @brief    Macro to automatically put a string constant into program memory
 */
#define uart_puts_P(__s)       uart_puts_p(PSTR(__s))



/** @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init */
extern void uart1_init(unsigned int baudrate, unsigned char format);
/** @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
extern unsigned int uart1_getc(void);
/** @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
extern void uart1_putc(unsigned char data);
/** @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
extern void uart1_puts(const char *s );
/** @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
extern void uart1_puts_p(const char *s );
/** @brief  Macro to automatically put a string constant into program memory */
#define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))

/**@}*/

typedef struct
{
	int 		intType;
	char		charType[2];
}int2byte;
#endif // UART_H 

